{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Constraining the total capacity per bus and carrier\n",
    "\n",
    "In this small example, we limit the nominal capacity of generators of the same production carrier at the same bus. \n",
    "\n",
    "Therefore, we introduce a column `nom_min_{carrier}` and `nom_max_{carrier}` in the `buses` dataframe. These are then used as lower and upper bounds of generators of the same carrier at the same bus. \n",
    "\n",
    "\n",
    "We start with importing a small example network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypsa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = pypsa.examples.ac_dc_meshed(from_master=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now add a second wind generator at bus 'Frankfurt' and limit the combined capacity. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n.add('Generator', 'Frankfurt Wind 2', bus='Frankfurt', capital_cost = 120,\n",
    "      carrier='wind', p_nom_extendable=True)\n",
    "\n",
    "n.buses.loc[['Frankfurt', 'Manchester'], 'nom_min_wind'] = 2000\n",
    "n.buses.loc[['Frankfurt'],'nom_max_wind'] = 2200"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are running the lopf and check whether the constraint is fulfilled. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n.lopf(pyomo=False)\n",
    "\n",
    "print(n.generators.p_nom_opt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looks good! The generators of carrier 'wind' at bus 'Frankfurt' are just the limit of 2200 MW.  "
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}